package com.java3_9;

import java.util.Random;

public class java3_9 {
    //    public static void main(String[] args) {
    //两种多线程的方式
    //（1）使用提取共享资源的方式
    //wait + notify + notifyAll必须使用同步监视器才能使用
//        TotalDish totalDish = new TotalDish(0);
//
//        //创建两个实现类,并将单独提取出的共享资源当作参数放入，实现数据的共享
//        DishPut dishPut1 = new DishPut(totalDish);
//        DishTake dishTake1 = new DishTake(totalDish);
//
//        //将实现类放入构造器中
//        Thread thread1 = new Thread(dishPut1);
//        Thread thread2 = new Thread(dishPut1);
//        Thread thread3 = new Thread(dishTake1);
//        Thread thread4 = new Thread(dishTake1);
//
//        //开始线程
//        thread1.start();
//        thread2.start();
//        thread3.start();
//        thread4.start();
    //（2）在共享资源中写好所有内容，线程只是运行其增删的操作
    //线程直接使用创建好的方法进行while循环（不循环那么只会执行一次之后就关闭了）
//        Dish dish = new Dish(0);
//
//        new Thread("服务员1"){
//            @Override
//            public void run() {
//                while (true) {
//                    dish.delDish();
//                }
//            }
//        }.start();
//
//        new Thread("服务员2"){
//            @Override
//            public void run() {
//                while (true) {
//                    dish.delDish();
//                }
//            }
//        }.start();
//
//        new Thread("厨师1"){
//            @Override
//            public void run() {
//                while (true) {
//                    dish.addDish();
//                }
//            }
//        }.start();
//
//        new Thread("厨师2"){
//            @Override
//            public void run() {
//                while (true) {
//                    dish.addDish();
//                }
//            }
//        }.start();
    //run方法就是让线程做什么事情
    //    }
//    public static void main(String[] args) {
////        new Thread(new Runnable() {
////            @Override
////            public void run() {
////                for (int i = 1; i <= 100; i++) {
////                    if (i % 2 == 0)
////                        System.out.println(Thread.currentThread().getName() + "  " + i);
////                }
////            }
////        }, "子线程").start();
////        Thread.currentThread().setName("主线程");
////        for (int i = 1; i <= 100; i++) {
////            if (i % 2 == 1) {
////                System.out.println(Thread.currentThread().getName() + "  " + i);
////            }
////        }
//
//
//        //在一个类中创建好所有的方法，直接在线程中调用
////        PrintNumber printNumber = new PrintNumber(1);
////
////        new Thread("奇数线程"){
////            @Override
////            public void run() {
////                while (printNumber.getNumber() <= 100) {
////                    printNumber.printEvenNum();
////                }
////            }
////        }.start();
////
////        new Thread("偶数线程"){
////            @Override
////            public void run() {
////                while (printNumber.getNumber() <=100) {
////                    printNumber.printOddNum();
////                }
////            }
////        }.start();
////
////    }
//
//
//    }
    public static void main(String[] args) {
        //创建一个共享数据类，将共享数据类作为参数传入线程中
//        Account account = new Account("20240309", 0);
//
//        //创建两个实现类的对象
//        Husband husband = new Husband(account);
//        wife wife = new wife(account);
//
//        //创造两个线程
//        Thread threadHusband = new Thread(husband,"丈夫");
//        Thread threadWife = new Thread(wife,"妻子");
//
//        threadHusband.start();
//        threadWife.start();

        MyAccount myAccount = new MyAccount("202040309",0);
        Random r = new Random();

        Thread thread1 = new Thread("丈夫") {
            @Override
            public void run() {
                while (true) {
                    myAccount.save(r.nextInt(10000) + 100);
                }
            }
        };

        Thread thread2 = new Thread("妻子") {
            @Override
            public void run() {
                while (true) {
                    myAccount.withdraw(r.nextInt(20000) + 2000);
                }
            }
        };

        thread1.setPriority(3);
        thread2.setPriority(10);

        thread2.start();
        thread1.start();

    }
}
